//
//  CSGXHomeSaleCategoryView.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/10/25.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXHomeSaleCategoryView.h"
#import "CSGXBubbleView.h"

@interface CSGXHomeSaleCategoryView ()

@property (nonatomic,weak) UIView *vLine;


@property(nonatomic, strong) UIButton * btnSelected;

@property (nonatomic, strong) NSArray<UIButton *> *buttonArr;

@property (nonatomic,weak) CSGXBubbleView *vBubble;

@property (nonatomic, strong) NSArray<CSGXBubbleView *> *vBubbles;
@property (nonatomic, strong) NSArray<UIColor *> *bubbleColors;



@end

@implementation CSGXHomeSaleCategoryView


+ (instancetype)gxCategoryViewWithDelegate:(id<CSGXCategoryViewActionDelegate>)delegateObj page:(NSInteger)page {
    id obj = [[self alloc] init];
    ((CSGXHomeSaleCategoryView *)obj).categoryActionDelegate = delegateObj;
    ((CSGXHomeSaleCategoryView *)obj).page = page;
    return obj;
}

#pragma mark - Actions

- (void)btnClick:(UIButton *)sender {
    //    NSLog(@"CSGXHomeSaleCategoryView: btnClick");
    
    NSInteger tagN = sender.tag;
    // 按钮不可用
    if (tagN == self.unSelectIndex) {
        return;
    }
    _segmentIndex = tagN;
    
    self.btnSelected.selected = NO;
    self.btnSelected.backgroundColor = kColorCategoryButtonGray;
    
    sender.selected = YES;
    sender.backgroundColor = kColorBar;
    self.btnSelected = sender;
    
    for (int i = 0; i < self.vBubbles.count; i++) {
        self.vBubbles[i].color = self.bubbleColors[i];
    }
    
    
    // call back
    if ([self.categoryActionDelegate respondsToSelector:@selector(gxCategoryView:withSelectedIndex:)]) {
        [self.categoryActionDelegate gxCategoryView:self withSelectedIndex:sender.tag];
    }
}


#pragma mark - initUI
- (void)initUI {
    self.unSelectIndex = -1;
    
    if (self.segments.count <= 0) {
        return;
    }
    
    if (self.isLine) {
        UIView *vLine = [[UIView alloc] init];
        [self addSubview:vLine];
        
        vLine.backgroundColor = self.lineColor;
        
        [vLine mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.bottom.trailing.equalTo(self).mas_offset(0);
            make.height.mas_offset(2);
        }];
        self.vLine = vLine;
    }
    
    UIView *segementView = [[UIView alloc] init];
    [self addSubview:segementView];
    
    if (self.isLine) {
        [segementView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.trailing.equalTo(self).mas_offset(0);
            make.bottom.equalTo(_vLine.mas_top).mas_offset(0);
        }];
    }else {
        [segementView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(self).mas_offset(0);
        }];
    }
    
    
    NSMutableArray<UIButton *> * buttonArrM = [NSMutableArray array];
    for (int i = 0; i < self.segments.count; i ++) {
        UIButton * btn = [UIButton gx_textButton:self.segments[i] fontSize:14 normalColor:nil selectedColor:nil];
        [segementView addSubview:btn];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = kColorCategoryButtonGray;
        
        // Default: selectes first button
        if (i == 0) {
            self.btnSelected = btn;
            [self btnClick:btn];
            _segmentIndex = i;
        }
        
        [buttonArrM addObject:btn];
    }
    self.buttonArr = buttonArrM.copy;
    
    CGFloat btnWidth = [UIScreen mainScreen].bounds.size.width / self.segments.count * 1.0; //btnWidth
    
    if (self.segments.count == 1) {
        [_buttonArr[0] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(segementView).mas_offset(0);
        }];
        
    }else {
        //
        [_buttonArr[0] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.equalTo(segementView).mas_offset(0);
            make.width.mas_offset(@(btnWidth));
        }];
        
        for (int i = 1; i < buttonArrM.count; i++) {
            //
            [_buttonArr[i] mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.top.bottom.equalTo(_buttonArr[0]);
                make.leading.equalTo(_buttonArr[i-1].mas_trailing);
            }];
        }
    } //count >= 2  end
    [self layoutIfNeeded];
}


- (void)initBubble {
    if (self.buttonArr.count == 0 || self.buttonArr == nil) {
        return;
    }
    //
    NSMutableArray<CSGXBubbleView *> *vBubblesM = [NSMutableArray<CSGXBubbleView *> array];
    NSMutableArray<UIColor *> *mColors = [NSMutableArray<UIColor *> array];
    for (int i = 0; i < self.bubbleDicts.count; i++) {
        NSDictionary *dict = self.bubbleDicts[i];
        NSInteger bubbleNumber = [dict[@"bubbleNumber"] integerValue];
        NSInteger bubbleIndex = [dict[@"bubbleIndex"] integerValue];
        UIColor *bubbleColor = dict[@"bubbleColor"];
        
        bubbleColor = !bubbleColor ? [UIColor orangeColor] : bubbleColor;
        [mColors addObject:bubbleColor];
        
        if (bubbleNumber <= 0) {
            continue;
        }
        if (![self isInSegementNumber:bubbleIndex]) {
            continue;
        }
        
        //创建 bubbleNumber
        CSGXBubbleView *vBubble = [CSGXBubbleView bubbleView];
        [self.buttonArr[bubbleIndex] addSubview:vBubble];
        vBubble.userInteractionEnabled = NO;
        [vBubblesM addObject:vBubble];
        
        [vBubble mas_makeConstraints:^(MASConstraintMaker *make) {
            if ([NSString stringWithFormat:@"%lu", (long)bubbleNumber].length >= 3) {
                make.size.mas_offset(CGSizeMake(25, 22));
            } else {
                make.size.mas_offset(CGSizeMake(20, 18));
            }
            make.centerY.equalTo(self.buttonArr[bubbleIndex]);
            make.trailing.equalTo(self.buttonArr[bubbleIndex]).mas_offset(-1);
        }];
        [self layoutIfNeeded];
        
        //
        vBubble.numberStr = [NSString stringWithFormat:@"%lu", (long)bubbleNumber];
        vBubble.color = bubbleColor;
    }//for end
    self.vBubbles = vBubblesM.copy;
    self.bubbleColors = mColors.copy;
}


#pragma mark - methods

- (void)setWidth:(CGFloat)width withIndex:(NSInteger)index {
    if (index > self.segments.count - 1) {
        return;
    }
    [_buttonArr[index] mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(width);
    }];
    [self layoutIfNeeded];
}

- (BOOL)isInSegementNumber:(NSInteger)index {
    if (index <= -1 || index >= self.segments.count) {
        return NO;
    }
    return YES;
}


#pragma mark - setter & getter
- (void)setSegments:(NSArray<NSString *> *)segments {
    _segments = segments;
    
    //创建
    [self initUI];
}

- (void)setBubbleDicts:(NSArray<NSDictionary *> *)bubbleDicts {
    _bubbleDicts = bubbleDicts;
    
    [self initBubble];
}





@end
